package br.usp.icmc.biomac.client.activity;

import java.util.ArrayList;
import java.util.List;

import br.usp.icmc.biomac.client.ClientFactory;
import br.usp.icmc.biomac.client.place.ImagePlace;
import br.usp.icmc.biomac.client.view.ImageAnnotation;
import br.usp.icmc.biomac.client.view.ImageView;
import br.usp.icmc.biomac.client.view.ROIAnnotationCellTable;
import br.usp.icmc.biomac.client.view.ROIImageCellTable;
import br.usp.icmc.biomac.shared.ImageDicom;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.dilvan.aimTemplateViewer.client.AIMTemplateParser;
import br.usp.icmc.dilvan.aimTemplateViewer.client.ComponentsAIM;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aim.ImageAnnotationAIM;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ImageActivity extends AbstractActivity implements 
		ImageView.Presenter, 
		ImageAnnotation.Presenter, 
		ROIImageCellTable.Presenter,
		ROIAnnotationCellTable.Presenter,
		ComponentsAIM.Presenter {

	ClientFactory clientFactory;
	ImagePlace place;
	String token;
	
	public ImageActivity(ImagePlace place, ClientFactory factory) {
		this.place = place;
		this.clientFactory = factory;
		token = this.place.getToken();
	}
	
	/**
	 * Navigate to a new Place in the browser
	 */
	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	/**
	 * Invoked by the ActivityManager to start a new Activity
	 */
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		final ImageView view = clientFactory.getImagePlaceView();
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		clientFactory.getRpcService().getImages(token, new AsyncCallback<ArrayList<ImageDicom>>() {
			@Override
			public void onSuccess(ArrayList<ImageDicom> result) {
			//	clientFactory.sertImageDicom(result);
				view.setData(clientFactory.getImageDicom());
			}
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error fetching images");
			}
		});
	}

	@Override
	public void showImageAnnotation(List<ROI> rois) {
		final ImageAnnotation view = clientFactory.getImageAnnotationPlace();
		view.setPresenter(this);
		view.showAnnotation();
		
	}

	@Override
	public void goToAnnotationTab(ROI roi) {
		final ImageAnnotation view = clientFactory.getImageAnnotationPlace();
		view.setPresenter(this);
		view.getTabs().selectTab(0);
		view.setROI(roi);
		
	}

	@Override
	public void setROIs() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getTemplateContainer() {
		final ImageAnnotation view = clientFactory.getImageAnnotationPlace();
		view.setPresenter(this);
		view.setTemplatesContainers(AIMTemplateParser.getTemplateContainers());
	}

	@Override
	public void getTemplates(String container) {
		final ImageAnnotation view = clientFactory.getImageAnnotationPlace();
		view.setPresenter(this);
		view.setTemplates(AIMTemplateParser.getShortTemplates(container));
	}

	@Override
	public void getTemplate(String container, String template) {
		final ImageAnnotation view = clientFactory.getImageAnnotationPlace();
		view.setPresenter(this);
//		view.createForm(AIMTemplateParser.getTemplate(container, template).getComponents());
	}

	@Override
	public void addROI(int indexImage, ROI roi) {
		// TODO Auto-generated method stub
		//return false;
	}

	@Override
	public void rmvROI(int indexImage, int indexROI) {
		// TODO Auto-generated method stub
		//return false;
	}

	@Override
	public ArrayList<ImageDicom> getImages() {
		return clientFactory.getImageDicom();
	}

	@Override
	public void setImages(ArrayList<ImageDicom> images) {
		// TODO Auto-generated method stub	
	}

	@Override
	public void getImages(String series) {
		clientFactory.getRpcService().getImages(series, new AsyncCallback<ArrayList<ImageDicom>>() {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onSuccess(ArrayList<ImageDicom> result) {
				clientFactory.setImageDicom(result);
			}
		});
		//return clientFactory.getImageDicom();
	}

	@Override
	public void edtROI(int indexImage, int indexROI, String label) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getIndexImage() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setIndexImage(int index) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sendResponseForm(ImageAnnotationAIM result) {
		// TODO Save the result from the template form
		Window.alert("The annotation AIM was save with sucessful!");
	}
}
